package com.qingzhuge.pay.controller;

import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EAuthorityType;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.controller.BasePkLongController;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.pay.dto.request.SysPayPropertyRequestDto;
import com.qingzhuge.pay.dto.response.SysPayPropertyResponseDto;
import com.qingzhuge.pay.entity.SysPayProperty;
import com.qingzhuge.pay.service.ISysPayPropertiesService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author : zero.xiao
 * @description : 支付配置
 * @date :2018/6/8 0008 下午 14:49
 * @modified :
 */
@ASysLog(value = "支付配置")
@AResource(name = "支付配置", type = EResourceType.menu, catalog = "系统管理")
@Api(tags = "S支付配置")
@RestController
@RequestMapping("api/sys/auth/pay")
public class SysPayController extends BasePkLongController<SysPayProperty, ISysPayPropertiesService,
        SysPayPropertyRequestDto, SysPayPropertyResponseDto> {

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysPayPropertyResponseDto>>> queryPage(@RequestBody(required = false) SysPayPropertyRequestDto param) {
        return super.queryPage(param);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysPayPropertyResponseDto>> get(@PathVariable Long id) {
        return super.get(id);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysPayPropertyResponseDto>> save(@RequestBody SysPayPropertyRequestDto pay) {
        return super.update(pay);
    }


    @Override
    public ResponseEntity<ResponseBodyDto<SysPayPropertyResponseDto>> update(@RequestBody SysPayPropertyRequestDto pay) {
        return super.update(pay);
    }


    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @ApiOperation(value = "上传支付证书")
    @PostMapping("cert/{id}")
    @AResource(type = EResourceType.button, authority = EAuthorityType.UPLOAD)
    @PreAuthorize("hasRole('ROOT') or hasAnyAuthority('SYSTEM','ADMIN') and hasAuthority('UPLOAD')")
    public ResponseEntity<ResponseBodyDto<Object>> cert(@RequestParam("file") MultipartFile file,
                                                        @PathVariable Long id) {
        if (null != file) {
            try {
                service.cert(id, file.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return returnSuccess();
        } else {
            return returnFailed("请选择p12支付证书文件");
        }
    }
}
